***Note: replicators should set directory to open data 
*cd ""

use Pisa.dta, clear
  
  ****** FIGURE 8 *********
  
global control_i "Pisa Female Edu_mum  WEALTH  Contact_social Contact_area  i.CNTRYID"
global control_s "Urban Single_sex Size  Private i.Education i.ISCEDO"

quietly:  mixed D  c.Cog#c.Contact_school c.Cog#c.Inclusion  Cog  Contact_school Inclusion  $control_i $control_s  || CNTSCHID: Cog Contact_school  if EU==1, mle nostderr  
est store D_EU
estadd local CountryFE "Yes"
estadd local Individual controls "Yes"
estadd local  School controls "Yes"
local N_EU=  e(N)
su D if e(sample), mean
loc Mean_EU:di%8.2fc r(mean)

quietly:  mixed D c.Cog#c.Contact_school c.Cog#c.Inclusion  Cog  Contact_school Inclusion  $control_i $control_s  || CNTSCHID: Cog   Contact_school   , mle nostderr
est store D_oecd
estadd local CountryFE "Yes"
estadd local Individual controls "Yes"
estadd local  School controls "Yes"
local N_oecd=  e(N)
su D if e(sample), mean
loc Mean_oecd:di%8.2fc r(mean)
 
 
  grstyle init
 	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color      "0 0 0"    "170 170 170" 
    grstyle set graphsize 23cm 13cm 
coefplot (D_EU, label( "ESS sample" "{it:N}=`N_EU'")) (D_oecd, label( "All" "{it:N}=`N_oecd'")) , keep( c.Cog#c.Contact_school c.Cog#c.Inclusion Cog Contact_school Inclusion)  xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))   coeflabels( c.Cog#c.Contact_school = `" "Cognitive"   " sophistication" "{bf:x} contact" "'   c.Cog#c.Inclusion = `" "Cognitive"   "sophistication" "{bf:x} instruction" "' Cog= `""Cognitive" "sophistication"'   Contact_school= `""Intergroup" "contact"'  Inclusion= `""Liberal" "instruction"'  , labsize(vsmall) notick labgap(3))  groups( c.Cog#c.Contact_school  c.Cog#c.Inclusion = "{bf:Interaction effects}"  , nogap)   xtitle("Anti-immigration attitudes ", size(medsmall)) ylabel(,angle(vertical) labsize(small)) yline(2.5,  lpattern(dot) ) xscale(r(-.2 .2)) xlabel(-.2(.1).2) legend(position(12) rows(1) nobox span )  note("Individual-level controls include gender, maternal education, family wealth, social contact and country fixed effects." "School-level controls include student staff-ratio, urbanity status, size and type of school" "(general or vocational) and grade, private or public status." "Outcome means: European sample `Mean_EU' |  All `Mean_oecd'." , size(vsmall) span)   
graph save "Figure8a", replace 

 
 esttab  D_EU    D_oecd  using "b_4c.tex", replace se star(* 0.1 ** 0.05 *** 0.01) obslast collabels(, none)   wrap  legend  gaps nolz  booktabs mlabel("ESS sample" "All"  )  nonumbers  cells(b(star fmt(3)  ) se(fmt(3) par  )) compress label title(Discriminatory attitudes at school.)   keep(c.Cog#c.Contact_school c.Cog#c.Inclusion  Cog Contact_school  Inclusion )   coeflabels(c.Cog#c.Contact_school   `" "Cognitive sophistication"   "{bf: x}" "contact" "'   c.Cog#c.Inclusion   `" "Cognitive sophistication"   "{bf: x}" "instruction" "' Cog  `""Cognitive" "sophistication"'   Contact  `" "Intergroup" "contact" "'   Inclusion  `" "Liberal" "instruction" "'  )        stats(  CountryFE   Individual_controls  School_controls N  , fmt(a1 a2 a3   %9.0fc  )  labels( `"Country FE"'  `"Individual_controls"' `"School"'   `"Estimator"' `"N.obs"'   ) )n
  

quietly:  mixed PD c.Cog#c.Contact_school  c.Cog#c.Inclusion  Cog Contact_school Inclusion $control_i $control_s  || CNTSCHID: Cog  Contact_school  if EU==1, mle  nostderr
est store PD_EU
local N_EU=  e(N)
su PD if e(sample), mean
loc Mean_EU:di%8.2fc r(mean)

quietly:  mixed PD c.Cog#c.Contact_school c.Cog#c.Inclusion  Cog Contact_school Inclusion  $control_i $control_s  || CNTSCHID: Cog Contact_school , mle nostderr
est store PD_oecd
local N_oecd=  e(N)
su PD if e(sample), mean
loc Mean_oecd:di%8.2fc r(mean)

 
coefplot (PD_EU, label( "ESS sample" "{it:N}=`N_EU'")) (PD_oecd, label( "All" "{it:N}=`N_oecd'")) , keep(c.Cog#c.Contact_school c.Cog#c.Inclusion Cog Contact_school   Inclusion)  xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))   coeflabels(c.Cog#c.Contact_school = `" "Cognitive"   " sophistication" "{bf:x} contact" "'      c.Cog#c.Inclusion = `" "Cognitive"   "sophistication" "{bf:x} instruction" "' Cog= `""Cognitive" "sophistication"'   Contact_school= `""Intergroup" "contact"'  Inclusion= `""Liberal" "instruction"' , labsize(vsmall) notick labgap(3))  groups(c.Cog#c.Contact_school   c.Cog#c.Inclusion = "{bf:Interaction effects}"  , nogap)   xtitle("Discriminatory climate at school", size(medsmall)) ylabel(,angle(vertical) labsize(small)) yline(2.5,  lpattern(dot) ) xscale(r(-.2 .2)) xlabel(-.2(.1).2) legend(position(12) rows(1) nobox span )  note("Individual-level controls include gender, maternal and paternal education, country fixed effects." "School-level controls include student staff-ratio, urbanity status, financial/staffing issues, type of school" "(general or vocational) and grade, private or public status." "Outcome means: ESS sample `Mean_EU' |  All `Mean_oecd'." , size(vsmall) span) title("{bf:Perceptions of discrimination}")    
graph save "Figure8b", replace 
 
 
 
  esttab  PD_EU  PD_oecd  using "Tableb_4d.tex", replace se star(* 0.1 ** 0.05 *** 0.01) obslast collabels(, none)   wrap  legend  gaps nolz  booktabs mlabel("ESS sample" "All"  )   coeflabels(c.Cog#c.Contact_school   `" "Cognitive sophistication"   "{bf: x}" "contact" "'   c.Cog#c.Inclusion   `" "Cognitive sophistication"   "{bf: x}" "instruction" "' Cog  `""Cognitive" "sophistication"'   Contact_school  `" "Intergroup" "contact" "'   Inclusion  `" "Liberal" "instruction" "'  ) nonumbers  cells(b(star fmt(3)  ) se(fmt(3) par  )) compress label title(Discriminatory attitudes at school.)   keep(c.Cog#c.Contact_school c.Cog#c.Inclusion  Cog Contact_school  Inclusion )       stats(  CountryFE   Individual_controls  School_controls N  , fmt(a1 a2 a3 %9.0fc )  labels( `"Country FE"'  `"Individual_controls"' `"School"'   `"Estimator"' `"N.obs"'   ) ) 

  
  
          		   grstyle init
grstyle set graphsize 24cm 24cm
 graph combine  "Figure8a.gph" "Figure8b.gph",  cols(2) row(1) imargin(l=0 r=0 b=0) 
 graph export "Figure8.tif", replace
 erase "Figure8b.gph"
 erase "Figure8a.gph"
